﻿using Microsoft.Win32;
using SHKF.B2C.Mannage.AdminUI.Model;
using SHKF.B2C.Mannage.AdminUI.Network;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.YouZanYun.Model.Inter.Image;
using SHKF.B2C.YouZanYun.Model.Inter.ImgUpload;
using SHKF.B2C.YouZanYun.Model.Inter.MaterialCategory;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls; 
using System.Windows.Input;
using System.Windows.Media.Imaging;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;

namespace SHKF.B2C.Mannage.AdminUI.View.Yzy.Materia
{
    /// <summary>
    /// frmMateria.xaml 的交互逻辑
    /// </summary>
    public partial class frmMateria : BasePage
    { 
        private HttpClientYouZanYun httpYzy;
        private string authority_id;
        private long companySN;
        private long category_id; //图片分类
        private ObservableCollection<ImageItem> imageList;

        public frmMateria()
        {
            InitializeComponent();
        }

        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            this.imageList = new ObservableCollection<ImageItem>();
              
            this.icImageList.ItemsSource = this.imageList;
            this.httpYzy = GL.CreateYouZanYun();

            //加载公司信息
            this.LoadCompany();
        }

        private void btnSearchCompany_Click(object sender, RoutedEventArgs e)
        { 
            //加载公司信息
            this.LoadCompany();

        }

        private async void tvCompany_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (this.tvCompany.SelectedItem != null)
            {
                WQDTreeViewItem itemSelected = (WQDTreeViewItem)this.tvCompany.SelectedItem;
                if (itemSelected.DataContext is CompanyEntity)
                {
                    this.companySN = (int)((CompanyEntity)itemSelected.DataContext).SN;
                }
                else
                {
                    this.companySN = 0;
                }

                if (this.companySN == 0)
                {
                    MessageBox.Show("请选择公司");
                    return;
                }

                DataDto dto = await GL.HttpClientManage.ShopGetKdtId(this.companySN);
                if (dto.error == null)
                {
                    if (dto.data == "0")
                    {
                        MessageBox.Show("没有配置有赞云总部ID");
                    }
                    else
                    {
                        authority_id = dto.data;
                        this.LoadImageCategory();

                    }
                }
            }
        }

        private async void LoadCompany()
        {
            string CompanyName = this.txtCompany.Text;
            CompanyListDto dto = await GL.HttpClientManage.QueryCompanyList(CompanyName);

            TreeViewItem tviRoot = (TreeViewItem)this.tvCompany.Items[0];
            tviRoot.ItemsSource = null;

            if (dto.error == null)
            {
                foreach (CompanyEntity company in dto.data)
                {
                    WQDTreeViewItem treeViewItem = new WQDTreeViewItem();
                    treeViewItem.DataContext = company;
                    treeViewItem.Header = company.CompanyName;
                    tviRoot.Items.Add(treeViewItem);
                }
                tviRoot.IsExpanded = true;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private void tvCategory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            this.LoadImage();
        }
  
        private async void LoadImage()
        {
            var data = this.tvCategory.SelectData;
            if (data != null && data is MaterialCategoryData)
            {
                MaterialCategoryData data1 = (MaterialCategoryData)data;

                //加载图片
                category_id = data1.category_id;

                this.imageList.Clear();
                if (this.category_id > 0)
                {
                    ImageResult result = await this.httpYzy.GetMaterialImageList(this.category_id);
                    if (result.Items != null)
                    {
                        int total_count = result.paginator.total_count;
                        int page_size = result.paginator.page_size;
                        int page_count = (int)Math.Ceiling((double)total_count / (double)page_size);

                        foreach (ImageItem item in result.Items)
                        {
                            this.imageList.Add(item);
                        }

                        if (page_count > 1)
                        {
                            LoadPageAllImage(page_count, category_id);
                        }

                    }
                }

            }
        }
        private async void LoadImageCategory()
        {
            this.tvCategory.Items.Clear();

            int count = 0; //总图片数

            this.httpYzy.authority_id = this.authority_id;

            //未分组
            MaterialCategoryResult result = await this.httpYzy.GetMaterialCategory(2);
            if (result.data != null && result.data.Count > 0)
            {
                WQDTreeViewItem item2 = new WQDTreeViewItem();
                foreach (MaterialCategoryData data in result.data)
                {
                    item2.DataContext = data;
                    item2.Header = result.data[0].category_name + $"({data.count})";
                    tvCategory.Items.Add(item2);

                    count += data.count;
                }
            }

            //自定义
            result = await this.httpYzy.GetMaterialCategory(1);
            if (result.data != null && result.data.Count > 0)
            {
                WQDTreeViewItem item1 = new WQDTreeViewItem();
                item1.Header = "自定义分组";
                tvCategory.Items.Add(item1);

                foreach (MaterialCategoryData data in result.data)
                {
                    WQDTreeViewItem item3 = new WQDTreeViewItem();
                    item3.DataContext = data;
                    item3.Header = data.category_name + $"({data.count})";
                    item1.Items.Add(item3);

                    count += data.count;

                }

                item1.IsExpanded = true;
            }

        }
 
        //加载２页及以后的图片
        private async void LoadPageAllImage(int PageCount, long category_id)
        {
            for (int i = 2; i <= PageCount; i++)
            {
                //加载图片
                ImageResult result = await this.httpYzy.GetMaterialImageList(category_id, i);
                if (result.Items != null)
                {
                    foreach (ImageItem item in result.Items)
                    {
                        this.imageList.Add(item);
                    } 
                }
            }
        }

        private void dgvImage_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnAddImage_Click(object sender, RoutedEventArgs e)
        {
            if (category_id == 0)
            {
                MessageBox.Show("请选择分类");
                return;
            }
             
            ImgUploadParam param = new ImgUploadParam();
            param.category_id = category_id;

            string fileName = "";
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                fileName = openFileDialog.FileName;
            }

            if (fileName != "")
            {
                param.image = new System.IO.FileInfo(fileName);
                ImgUploadResult result = await this.httpYzy.ImgUpload(param);
                if (result.success)
                {
                    this.LoadImage();
                }
                else
                {
                    MessageBox.Show(result.message);
                }


            }
        }

      
    }
}
